﻿using Microsoft.AspNetCore.StaticFiles;

namespace Bigway.Common.Utils;

public static class FileUtils
{
    public static List<FileInfo> GetFileInfosOrderByCreationTime(string dir, 
        string searchPattern = "*",
        SearchOption searchOption = SearchOption.TopDirectoryOnly)
    {
        var files = Directory.GetFiles(dir, searchPattern, searchOption);
        return files.Select(x => new FileInfo(x))
            .OrderBy(x => x.CreationTimeUtc)
            .ToList();
    }

    public static string GetMimeType(string fileName)
    {
        var provider = new FileExtensionContentTypeProvider();
        if (!provider.TryGetContentType(fileName, out var contentType))
        {
            contentType = "application/octet-stream";
        }
        return contentType;
    }
}